package com.zzyl.chinamobileai.rag;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class Knowledge {
    @Autowired
    private VectorStore vectorStore;
    @PostConstruct
    private void Vector(){
        //知识库是一个Json格式数据
        String json = "[\n" +
                "  {\n" +
                "    \"id\": \"rule_01\",\n" +
                "    \"type\": \"合约期\",\n" +
                "    \"condition\": \"合约期未满12个月\",\n" +
                "    \"description\": \"提前更换套餐需支付剩余费用的10%作为违约金。\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id\": \"rule_02\",\n" +
                "    \"type\": \"携号转网\",\n" +
                "    \"condition\": \"申请携号转网\",\n" +
                "    \"description\": \"需满足：1. 无欠费 2. 当前套餐已到期 3. 非吉祥号段（如138/139开头）。\"\n" +
                "  }\n" +
                "]";
        //将知识库分割为document
        JSONArray objects = JSONUtil.parseArray(json);
        List<Document> documents = new ArrayList<>();
        objects.forEach(object -> {
            documents.add(new Document(object.toString()));
        });
        //添加进知识库向量中
        vectorStore.add(documents);
    }
}
